from existence import Existence
from interaction20 import Interaction20
from experimentoutput import pickOtherExperiment
from environmentinput import setEnvironment20, getResultFromEnv20

# Lesson 2 derived Existence class
class Existence20(Existence):
    def __init__(self, steps):
        super().__init__(steps)

    def createTestBench(self):
        setEnvironment20()
        Interaction20.clearInteractionDictionary()
        Interaction20.createPrimitiveInteraction('e1', 'r1', -1)
        Interaction20.createPrimitiveInteraction('e1', 'r2', 1)
        Interaction20.createPrimitiveInteraction('e2', 'r1', -1)
        Interaction20.createPrimitiveInteraction('e2', 'r2', 1)
        Interaction20.createPrimitiveInteraction('e3', 'r1', 1)
        Interaction20.createPrimitiveInteraction('e3', 'r2', -1)
        Interaction20.createPrimitiveInteraction('e4', 'r1', -1)
        Interaction20.createPrimitiveInteraction('e4', 'r2', 1)
        Interaction20.createPrimitiveInteraction('e5', 'r1', 1)
        Interaction20.createPrimitiveInteraction('e5', 'r2', 1)
        Interaction20.createPrimitiveInteraction('e6', 'r1', -1)
        Interaction20.createPrimitiveInteraction('e6', 'r2', 1)

    def start(self, randomFlag = False):
        self.createTestBench()

        experiment = ''
        mood = ''
        selfSatisfiedDuration = 0
        lastEnactedValence = 0
        result = ''
        cycle = 1

        while cycle < self.steps:
            if mood != 'PLEASED':
                experiment = pickOtherExperiment(experiment, randomFlag)
                result = ''

            result = getResultFromEnv20(experiment, result, randomFlag)
            enactedInteraction = Interaction20.getEnactedInteraction(experiment, result)

            if int(enactedInteraction.valence) >= 0:
                selfSatisfiedDuration = 0 if lastEnactedValence < 0 else selfSatisfiedDuration
                lastEnactedValence = enactedInteraction.valence
                selfSatisfiedDuration += 1
                if selfSatisfiedDuration >= 3:
                    mood = 'BORED'
                else:
                    mood = 'PLEASED'
            else:
                selfSatisfiedDuration = 0 if lastEnactedValence >= 0 else selfSatisfiedDuration
                lastEnactedValence = enactedInteraction.valence
                selfSatisfiedDuration -= 1

                if selfSatisfiedDuration <= -3:
                    mood = 'FRUSTRATED'
                else:
                    mood = 'PAINED'

            print(enactedInteraction, mood)
            cycle += 1